﻿<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8" />
    <title>SignalR Simple Chat</title>
    <style>
        .container{
            background-color: #99ccff;
            border: thick solid #808080;
            padding: 20px;
            margin: 20px;
        }
    </style>
</head>
<body>
    <div class="container">
        <input type="text" id="message" />
        <input type="button" id="sendmessage" value="Send" />
        <input type="hidden" id="displayname" />
        <ul id="discussion"></ul>
    </div>
    <script src="Scripts/jquery-3.3.1.min.js"></script>
<script src="Scripts/jquery.signalR-2.4.0.min.js"></script>
<script src="http://localhost:8080/signalr/hubs"></script>
<script>
    $(function() {
        $.connection.hub.url = "http://localhost:8080/signalr";
        var chat = $.connection.myHub;

        chat.client.addMessage = function(name, message) {
            var encodedName = $("<div/>").text(name).html();
            var encodedMsg = $("<div/>").text(message).html();
            $("#discussion").append(`<li><strong>${encodedName}</strong>:&nbsp;&nbsp;${encodedMsg}</li>`);
        };
        $("#displayname").val(prompt("enter your name:", ""));
        $("#message").focus();
        $.connection.hub.start().done(function() {
            $("#sendmessage").click(function() {
                chat.server.send($("#displayname").val(), $("#message").val());
                $("#message").val("").focus();
            });
        });
    })
</script>
</body>
</html>